import React, { Component } from 'react'
import Taro,{Current} from "@tarojs/taro"
import { View } from '@tarojs/components'
import { AtTabs, AtTabsPane  } from "taro-ui"
import {HospitalItems} from '../../../components/view/HospitalItems'
import {DoctorItems}from '../../../components/view/DoctorItems'
import {api_get_doctors_hospitals_by_input} from '../../../utils/api'

export default class searchResult extends Component {

  constructor(){
    super(...arguments)
    this.state = {
      loading:false,
      getting:false,
      data:'',
      current:0
    }
  }

  componentWillMount(){
    // 界面传参
    this.setState({
      city:Current.router.params.city,
      input:Current.router.params.input,
      // city:'武汉市',
      // input:'中',
      getting:true
    })
    // 加载数据
    Taro.showLoading({
      title: '加载中',
    })
  }

  myRequest =  () => {
    const params = {
      url: api_get_doctors_hospitals_by_input,
      data: {
        city:this.state.city,
        input:this.state.input
      },
      method: 'POST',
      header: {'content-type': 'application/x-www-form-urlencoded'},
      success: (res) => {
        if(res.data.hospital_number==0 && res.data.doctor_number>0){
          this.setState({
            data:res.data,
            loading:true,
            current:1
          }) 
        }else{
          this.setState({
            data:res.data,
            loading:true
          }) 
        }
      },
      fail: (res) => {console.log(res.data)}
    }
    Taro.request(params)
  }

  handleClick (value) {
    this.setState({
      current: value
    })
  }

  render () {
    if(this.state.getting && !this.state.loading){
      this.myRequest()
    }
    if(this.state.loading ){
      // console.log(this.state.data);
      setTimeout(function () {Taro.hideLoading()}, 100)
    }
    const tabList = [{ title: '医院('+this.state.data.hospital_number+')' }, { title: '医生('+this.state.data.doctor_number+')' }]
    return(
      this.state.loading?
      <AtTabs current={this.state.current} tabList={tabList} onClick={this.handleClick.bind(this)}>
      <AtTabsPane current={this.state.current} index={0} >
        <View style='padding: 10px 10px;background-color: #FAFBFC;text-align: center;' >
          <HospitalItems data={this.state.data.hospitalCutInforList}></HospitalItems>
        </View>
      </AtTabsPane>
      <AtTabsPane current={this.state.current} index={1}>
        <View style='padding: 10px 10px;background-color: #FAFBFC;text-align: center;'>
          <DoctorItems data={this.state.data.doctorHospitalInforList}></DoctorItems>
        </View>
      </AtTabsPane>
    </AtTabs>
      :<View></View>
    )
  }
}

